import { CHANGE_CURRENT, GET_ALL_GOODS, SEARCH_GOOD_BY_ID } from '@/store/goods/const'
import { Commit } from 'vuex'
import { State } from '@/store/goods/interface'
import { getAllGoods, searchGoodById } from '@/api/goods'

export default {
  async [GET_ALL_GOODS] (context:{commit:Commit, state:State}) {
    const { state } = context
    const { data } = await getAllGoods({ pagenum: state.limit.pagenum, pagesize: state.limit.pagesize })
    context.commit(GET_ALL_GOODS, data)
  },
  async [SEARCH_GOOD_BY_ID] (context:{commit:Commit, state:State}, id:number) {
    const { commit, state } = context
    const { data } = await searchGoodById(id)

    commit(GET_ALL_GOODS, { total: state.count, pagenum: state.limit.pagenum, goods: [data] })
  }
}
